//定时任务
const cron = require("node-cron");
//哈希值
// const crypto = require('crypto');
// const axios = require('axios');
const { log } = require("console");
//api 请求
const { login, getSnData, uploadShipData } = require("../utils/api/hskj");

function nodeCron(params) {
  // 定时任务：每分钟执行一次（Cron表达式 "0 * * * * *" 表示每分钟的第0秒）
  cron.schedule("*/5 * * * * *", async () => {
    //登录获取token
    let loginRes = await login({ account: "admin2", password: "123456" });
    //储存token
    let token = loginRes.data.data.token;

    let getSnDataRes = await getSnData({
      token: loginRes.data.data.token,
      sn: "211",
    });

    //番禺大桥数据
    let ais = getSnDataRes.data.data;

    //处理成log格式
    ais.forEach((element) => {
      element.time = element.time * 1000;
    });
    try {
      let data = [
        {
          BridgeCode: "S4440105L1234",
          DataTimeUnix: time,
          Mmsi: "412000000",
          ChineseName: "粤清远货2962",
          EnglishName: "YUEQINGYUANHUO2962",
          Longtitude: 113.4774033,
          Latitude: 22.73047,
          Cog: 244.2,
          Sog: 5.4,
          ShipType: 50,
        },
      ];

      // let res =  generateSignature(data,1742375476)

      let AddNearbyShipRes = await uploadShipData(data, 1742437326);

      console.log(42, AddNearbyShipRes);
    } catch (error) {
      console.log(45, error);
    }
  });
}

// 导出定时任务
module.exports = { nodeCron };
